<template>
  <div>
    <!-- 面包屑导航区域 -->
    <bread-crumb :breadContent="breadContent"></bread-crumb>

    <!-- 搜索与添加区域 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入内容" v-model="queryInfo.key" clearable @clear="getResourcesList">
          <el-button slot="append" icon="el-icon-search" @click="getResourcesList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="addDialogVisible = true">添加资源</el-button>
      </el-col>
    </el-row>
    <br/>

    <!-- 卡片视图 -->
    <el-card>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane v-for="item in resourceType" :label="item" :name="item"></el-tab-pane>
        <!-- <el-tab-pane label="menu-菜单资源" name="menu"></el-tab-pane>
        <el-tab-pane label="warehouse-仓库资源" name="warehouse"></el-tab-pane>
        <el-tab-pane label="organization-组织资源" name="organization"></el-tab-pane>
        <el-tab-pane label="specialLine-专业线资源" name="specialLine"></el-tab-pane> -->
      </el-tabs>
      <el-table :data="resourceList" border stripe :max-height="pageHeight">
        <el-table-column align="center" type="index"></el-table-column>
        <el-table-column align="center" label="资源描述" prop="description"></el-table-column>
        <el-table-column align="center" label="资源类型" prop="type"></el-table-column>
        <el-table-column align="center" label="绑定的具体资源" prop="resource.name"></el-table-column>
        <el-table-column width="100px" label="操作" align="center">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="删除资源" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeResourceById(scope.row.id)" ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <pagination  
      v-show="total>0" 
      :total="total" 
      :page.sync="queryInfo.page" 
      :limit.sync="queryInfo.size" 
      @pagination="getResourcesList"/>
    </el-card>

    <!-- 添加资源的对话框 -->
    <el-dialog title="添加资源" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" ref="addFormRef" label-width="80px" :rules="addFormRules">
        <el-form-item label="资源类型" prop="type">
          <el-select v-model="addForm.type" clearable placeholder="请选择" style="width:100%" @change="selectChanged">
            <el-option
              v-for="item in resourceType"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="可绑资源" prop="refId">
          <el-select v-model="addForm.refId" clearable placeholder="请选择" style="width:100%" @change="selectChangedItem">
            <el-option
              v-for="item in bindableResourceList"
              :key="item.id"
              :label="item.description"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addResource">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        activeName: 'menu',
        breadContent:["超管设置","资源管理"],
        resourceList:[],
        resourceMenuList:[],
        resourceWarehouseList:[],
        resourceOrganizationList:[],
        queryInfo: {
          key: '',
          type:'menu',  // 资源类型
          page: 1,  // 当前的页数
          size: 10  // 当前每页显示多少条数据
        },
        total: 0,
        addDialogVisible: false,
        // 添加资源的表单数据
        addForm: {
          description:'',
          type:'',
          refId:''
        },
        // 资源类型
        resourceType:[],
        // 可绑定资源列表
        bindableResourceList:[],
        // 添加表单的验证规则对象
        addFormRules: {
          type: [
            { required: true, message: '请选择资源类型', trigger: 'change'},
          ],
          refId: [
            { required: true, message: '请选择要绑定的资源', trigger: 'change'},
          ]
        },
      }
    },
    created(){
      this.getResourcesList()
      this.getResourcesTypeList()
    },
    methods:{
      // 获取资源列表
      async getResourcesList(){
        const { data: res } = await this.$http.get('resource/page',{params: this.queryInfo})
        if (res.status !== 200) {
          return this.$message.error(res.message)
        }
        console.log(res.data.records)
        this.resourceList = res.data.records
        this.total = res.data.total
      },
      // 获取资源类型列表
       async getResourcesTypeList(){
        const { data: res } = await this.$http.get('resource/list/type')
        if (res.status !== 200) {
          return this.$message.error(res.message)
        }
        console.log(res.data)
        this.resourceType = res.data
      },
      // 监听添加资源对话框的关闭事件
      addDialogClosed () {
        this.$refs.addFormRef.resetFields()
        this.bindableResourceList = []
        this.addForm.refId = ''
      },
      // 添加资源对话框中select选中的事件
      async selectChanged(value){
        const { data: res } = await this.$http.get('/resource/list/ref',{params: {type:value}})
        if (res.status !== 200) {
          return this.$message.error(res.message)
        }
        this.bindableResourceList = res.data
      },
      // 添加资源
      addResource(){
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return
          const { data: res } = await this.$http.post('/resource/add',this.addForm)
          if (res.status !== 201) {
            return this.$message.error(res.message)
          }
          this.$message.success("添加资源成功！")
          this.addDialogVisible = false
          this.getResourcesList()
          this.$refs.addFormRef.resetFields()
        })
      },
      // 根据ID删除资源
      async removeResourceById(id){
        // 弹框询问角色是否删除数据
        const confirmResult = await this.$confirm(
          '此操作将永久删除该资源, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).catch(err => err)
        // console.log(confirmResult)
        if (confirmResult !== 'confirm') {
          return this.$message.info('已取消删除')
        }
        const { data: res } = await this.$http.delete('resource/' + id)
        if (res.status !== 200) {
          return this.$message.error('删除资源失败！')
        }

        this.$message.success('删除资源成功！')
        this.getResourcesList()
      },

      // 添加资源对话框中可绑资源select选中的事件
      async selectChangedItem(value){
        console.log(value)
        console.log(this.bindableResourceList)
        this.bindableResourceList.forEach(item => {
          if(item.id === value){
            this.addForm.description = item.description
          }
        })
      },

      // 点击 tabs 标签页的事件
      async handleClick(tab, event) {
        console.log(tab, event);
        // 根据点击的tabs的name获取指定类型的资源列表
        this.queryInfo.type = tab.name
        this.getResourcesList()
      }
    } 
  }
</script>

<style lang="less" scoped>

</style>